package test.administrator.example.com.mvptest1.framework.support.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import test.administrator.example.com.mvptest1.framework.MvpPresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.framework.support.MvpDelegateCallback;
import test.administrator.example.com.mvptest1.framework.support.fragment.FragmentMvpDelegate;
import test.administrator.example.com.mvptest1.framework.support.fragment.FragmentMvpDelegateImpl;

/**
 * 讲解一下：集成MVP的时候->和我们的Activity类似
 * 第一重代理：生命周期->代理对象
 * 第二重代理：绑定和解绑->目标对象
 * Created by thinkpad on 2018/7/30.
 */

public abstract class MvpFragment<V extends MvpView, P extends MvpPresenter<V>>
		extends Fragment implements MvpView, MvpDelegateCallback<V, P> {

	//持有目标对象引用
	protected FragmentMvpDelegate<V, P> mvpDelegate;

	protected FragmentMvpDelegate<V, P> getMvpDelegate() {
		if (this.mvpDelegate == null) {
			this.mvpDelegate = new FragmentMvpDelegateImpl<V, P>(this);
		}
		return this.mvpDelegate;
	}

	/****第二重代理****/
	private P presenter;

	@Override
	public P createPresenter() {
		return presenter;
	}

	@Override
	public P getPresenter() {
		return presenter;
	}

	@Override
	public void setPresenter(P presenter) {
		this.presenter = presenter;
	}

	@Override
	public V getMvpView() {
		return (V) this;
	}

	/****结束****/

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		getMvpDelegate().onAttach(context);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getMvpDelegate().onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getMvpDelegate().onActivityCreated(savedInstanceState);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getMvpDelegate().onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onStart() {
		super.onStart();
		getMvpDelegate().onStart();
	}

	@Override
	public void onResume() {
		super.onResume();
		getMvpDelegate().onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		getMvpDelegate().onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
		getMvpDelegate().onStop();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		getMvpDelegate().onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getMvpDelegate().onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
		getMvpDelegate().onDetach();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getMvpDelegate().onSaveInstanceState(outState);
	}

}






























































































